<template>
		<view class="container">
			<view class="top">
				<image class="image" src="https://m.nocexpo.com/statics/js/modules/h5/img/beijing.png"></image>
				<image  class="jinnuo" :src="imgUrl + '/statics/js/modules/applet/img/logo_square.png'" mode="widthFix"></image>
			</view>
		<view class="box">
			<button class="btn" style="color: #fff;"
			    type="default" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">手机号快捷登录</button>
				<view style="color: #5c5c5c; padding-left:20px; padding-top: 20px; width: 660rpx;" >
				  <radio-group @change="isChecked=!isChecked" class="phoneRadio form-radio_wrap" >
				    <label class="radio">
				      <radio :checked="isChecked"></radio>
				        已阅读并同意<text style="color: #2EC2BE">《中国移动认证服务条款》</text>和<text style="color: #00AAA6;">《用户注册协议》</text>并使用本机号码登录
				    </label>
				  </radio-group>
				</view>
		</view>
		<view class="link login">
			   <image  class="tu3" :src="imgUrl + '/statics/js/modules/applet/img/denglufangshi.png'" mode=""></image>
			<view @click="shoujidenglu">
			   <image  class="tu1" :src="imgUrl + '/statics/js/modules/applet/img/shoujima.png'" mode=""></image>
				<navigator class="url" open-type="redirect"  hover-class="none">手机验证码登录</navigator>
			</view>
			<view @click="mimadenglu">
				<navigator class="ur2" open-type="redirect"  hover-class="none">手机密码登录</navigator>
				<image  class="tu2" :src="imgUrl + '/statics/js/modules/applet/img/mima.png'" mode=""></image>
			</view>
		</view>
		</view>
</template>

<script>
import login from '@/api/login.js';
import commonApi from '@/utils/commonMethod.js';
import indexApi from '@/utils/request/index.js';

export default {
  data() {
    return {
      countdown: 60,
      demandId: null,
      nocUser: {
        phone: '',
        password: '',
        verificationCode: '',
        isChecked: false
      },
      sendFlag: true,
      sendMsg: '获取验证码',
      currentUser: {},
      imgUrl: indexApi.baseUrl

    };
  },
  onLoad(options) {
    this.url = options.url;
    if (options.demandId) {
      this.demandId = options.demandId
    }
    console.log(this.demandId)
  },
  mounted() {
    let phone = '';
    if (phone) {
      this.nocUser.phone = phone
    }
  },
  methods: {
    //手机验证码跳转
    shoujidenglu() {
      uni.navigateTo({
        url: '../../../pages/account/register/yanzhma'
      })
    },
    //手机密码跳转
    mimadenglu() {
      uni.navigateTo({
        url: '../../../pages/account/login/loginBy'
      })
    },
    //登陆方法
    otherLogin() {
      //调用登录方法
      that.login(this.nocUser.phone, this.nocUser.verificationCode);
    },
    getCheckCode(e) {
      if (!this.sendFlag) {
        return;
      }
      this.nocUser.phone = this.nocUser.phone.trim();
      //验证用户是否存在
      login.getCheckCode(
          {phone: this.nocUser.phone}
      ).then(response => {
        if (response.data.status == 1) {
          login.slideCheck(this.nocUser.phone);
        } else if (response.data.status == 2) {
          layer.open({
            content: '账号已被封禁，请联系平台客服',
            btn: ['确定'],
            yes: function (index) {
              layer.close(index)
            }
          });
        } else {
          layer.open({
            anim: '提示',
            content: '该账号不存在,请注册',
            btn: ['立即注册'],
            yes: function (index) {
              layer.close(index)
              this.toRegistered()
              return;
            }
          });
        }
      }).catch(function (error) {
        uni.showToast({
          "title": error,
          "icon": "none",
        });
      });


    },
    //验证码定时器,60秒后可以再次获取
    settime(e) {
      if (this.countdown == 0) {
        vm.sendMsg = "获取验证码";
        vm.sendFlag = true;
        this.countdown = 60;
        return;
      } else {
        vm.sendMsg = "重新获取" + this.countdown + "s";
        vm.sendFlag = false;
        this.countdown--;
      }
      setTimeout(function () {
        vm.settime(e)
      }, 1000)
    },

    //跳转到注册页面
    toRegistered() {
      if (vm.nocUser.phone) {
        window.location.href = baseURL + "/h5/registered.html?phone=" + vm.nocUser.phone
      } else {
        window.location.href = baseURL + "/h5/registered.html"
      }
    },

    // 获取手机号
    getPhoneNumber(e) {
      let that = this;
      if (!this.isChecked) {
        uni.showToast({
          "title": '请先同意协议',
          "icon": "none",
        });
        return;
      }
      login.getPhoneNumber({code: e.detail.code}).then(function (res) {
        if (res.data.code == 0) {
          //手机号
          that.nocUser.phone = res.data.phonenumber;
          //调用登录方法
          that.login(that.nocUser.phone, '052930').then(res => {

          });

        }
      })

    },
    getCurrentUser() {
      console.log("getCurrentUser");
      commonApi.currentUserAxios().then(res => {
        console.log(res);
        console.log(this);
        this.currentUser = res.data.session_user_h5;
        commonApi.updUserInfo(this.currentUser.id, this.currentUser.type);
		commonApi.updLogUserLogin(this.currentUser);
        if (this.demandId == null) {
          if (!this.url) {
            uni.switchTab({
              url: '/pages/tabBar/index/index'
            })
          }
          if (this.url.includes("list") || this.url.includes("memberDemand")) {
            //window.location.href = baseURL + this.url
            uni.redirectTo({
              url: this.url
            })
          } else {
            //window.location.href = baseURL + "h5/index.html"
            uni.switchTab({
              url: '/pages/tabBar/index/index'
            })
          }
        } else {
          uni.redirectTo({
            url: '/pages/need/detail/detail?demandId=' + this.demandId
          })
        }

      });
    },

    // 公共登录方法
    login(phone, verificationCode) {
      let that = this
      //登录
      //	todo 表单验证功能待做
      login.login(
          '',
          {
            phone: phone,
            verificationCode: verificationCode,
			appletSourceUserId : uni.getStorageSync("sourceId")
          }
      ).then(function (response) {
        if (response.data.code === 0) {
          console.log(response.cookies)
          //记录cookies
          if (response.cookies && response.cookies.length > 0) {
            for (let i = 0; i < response.cookies.length; i++) {
              let cookie = response.cookies[i];
              if (cookie.indexOf('SESSION') >= 0) {
                uni.setStorageSync('cookie', cookie);
              }
            }
          }

          //登录后，获取用户存入登录记录表
          that.getCurrentUser();
          //登录后清除弹窗状态
          //localStorage.removeItem("ifPop");
          if (!this.url) {
            uni.switchTab({
              url: '/pages/tabBar/index/index'
            })
          }
          if (this.url.includes("/h5login")) {
            uni.switchTab({
              url: '/pages/tabBar/index/index'
            })
          } else if (this.url.includes("list") || this.url.includes("memberDemand")) {
            //window.location.href = baseURL + this.url
            uni.redirectTo({
              url: this.url
            })
          }
        } else {
          layer.open({
            anim: '提示',
            content: response.data.msg,
            btn: ['确定'],
            yes: function (index) {
              layer.close(index)
            }
          });
        }
      }).catch(function (error) {
      });
    }
  }
};
</script>

<style lang="scss" scoped>
@import '../account.scss';
</style>
